package com.ratta.supernote.plugin.sticker.lib.view.groupList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/// 虚线分割
public class DotItemDecoration extends RecyclerView.ItemDecoration {

    private Paint mPaint = new Paint();
    private Path path = new Path();
    private float dashWidth;   // 虚线线段宽度
    private float gapWidth;    // 空白间隔宽度
    private int lineColor;
    private int mDividerHeight;
    private int paddingH;

    public DotItemDecoration(Context context) {
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        // 默认值
        dashWidth = context.getResources().getDimensionPixelOffset(com.ratta.supernote.note.plugincore.R.dimen.dp_px_2);
        gapWidth = context.getResources().getDimensionPixelOffset(com.ratta.supernote.note.plugincore.R.dimen.dp_px_4);
        lineColor = 0xFF000000;
        mDividerHeight = context.getResources().getDimensionPixelOffset(
                com.ratta.supernote.note.plugincore.R.dimen.dp_px_2
        );
        paddingH = context.getResources().getDimensionPixelSize(com.ratta.supernote.note.plugincore.R.dimen.dp_px_48);


    }

    @Override
    public void onDraw(@NonNull Canvas canvas, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();

        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount - 1; i++) {
            View child = parent.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

            int top = child.getBottom() + params.bottomMargin;


            mPaint.setColor(lineColor);
            // 添加dp转px逻辑
            mPaint.setStrokeWidth(1);

            // 创建虚线效果
            float[] intervals = {2, 5};
            mPaint.setPathEffect(new DashPathEffect(intervals, 0));
            // 水平虚线
            path.moveTo(left + paddingH, top + mDividerHeight / 2);
            path.lineTo(right - paddingH, top + mDividerHeight / 2);
            canvas.drawPath(path, mPaint);
            path.reset();

        }
    }

//    @Override
//    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
//        super.getItemOffsets(outRect, view, parent, state);
//        outRect.bottom = view.getContext().getResources().getDimensionPixelOffset(
//                com.ratta.supernote.note.plugincore.R.dimen.dp_px_2
//        );
//    }
}
